package com.lagou.homework.stage01.module3.Task4;

import java.util.*;

public class StudentTest {
    static Map<Integer,Student> stdMap = new HashMap<>();
    static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {

        while(true) {
            System.out.println("请输出你要对学生信息进行的操作：（1.增加、2.删除、3.修改、4.查找、5.遍历、6.退出）");
            int a = sc.nextInt();

            StudentTest st = new StudentTest();
            switch (a) {
                case 1:
                    st.addStudent();
                    break;
                case 2:
                    st.delStudent();
                    break;
                case 3:
                    st.setStudent();
                    break;
                case 4:
                    st.seekStudent();
                    break;
                case 5:
                    st.listStudent();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("输入错误 ！");
                    break;
            }
        }
    }

    private void addStudent(){
        System.out.println("请输入要添加的学生信息（学号，姓名，年龄）：");
        int stdId = sc.nextInt();
        String stdName = sc.next();
        int stdAge = sc.nextInt();

        if(stdMap.containsKey(stdId)){
            System.out.println("该学号已存在，添加失败！");
            return;
        }

        Student std = new Student(stdName,stdAge);
        stdMap.put(stdId,std);
    }
    private void delStudent() {
        System.out.println("请输入学生学号：");
        int stdId = sc.nextInt();

        if (stdMap.containsKey(stdId)) {
            stdMap.remove(stdId);
        } else {
            System.out.println("该学生不存在，删除失败 ！");
        }
    }

    private void setStudent(){
        System.out.println("请输入学生学号：");
        int stdId = sc.nextInt();

        if(stdMap.containsKey(stdId)) {
            System.out.println("请输入该学生的姓名和年龄：");
            String stdName = sc.next();
            int stdAge = sc.nextInt();

            Student std = new Student(stdName,stdAge);
            stdMap.put(stdId,std);

        }else{
            System.out.println("不存在该学生 ！");
        }
    }

    private void seekStudent(){
        System.out.println("请输入学生学号：");
        int stdId = sc.nextInt();

        if(stdMap.containsKey(stdId)){
            System.out.println(stdMap.get(stdId));
        }else{
            System.out.println("不存在该学生 ！");
        }
    }
    private void listStudent(){
        Iterator it = stdMap.entrySet().iterator();

        while(it.hasNext()){
            Map.Entry pairs = (Map.Entry)it.next();
            System.out.println("学号： "+pairs.getKey() + " 学生信息： " + pairs.getValue());
        }
    }
}
